package henu.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import henu.bean.FeedBack;
import henu.bean.Teacher;
import henu.factory.DaoFactory;

public class FeedBackAction extends ActionSupport {
	HttpServletRequest request = null;
	public String add(){
		request = ServletActionContext.getRequest();
		int n = 0;
		String classNum = request.getParameter("classNum");
		String lesson = request.getParameter("lesson");
		String teacher = request.getParameter("teacher");
		String weekTime = request.getParameter("weekTime");
		String time = request.getParameter("time");
		String schedule = request.getParameter("schedule");
		String classSitutation = request.getParameter("classSitutation");
		String classEfect = request.getParameter("classEfect");
		String afterClassSitu = request.getParameter("afterClassSitu");
		String memo = request.getParameter("memo");
		String advice = request.getParameter("advice");
		FeedBack fb = new FeedBack();
		fb.setfSubjectNum(classNum);
		fb.setfLessonName(lesson);
		fb.setfTeacherName(teacher);
		fb.setfWeekTime(weekTime);
		fb.setfClassOrd(time);
		fb.setfSchedule(schedule);
		fb.setfClassSitutation(classSitutation);
		fb.setfClassEfect(classEfect);
		fb.setfAfterClassSitu(afterClassSitu);
		fb.setfAdvice(advice);
		fb.setfMemo(memo);
		n = DaoFactory.createFeedBackDao().save(fb);
		System.out.println(n);
		if(n == 1){
			return SUCCESS;
		}else{
			return ERROR;
		}
	}
	
	public String getAll(){
		//int pageNow = Integer.parseInt(request.getParameter("num"));
		List<FeedBack> lists = null;
		request = ServletActionContext.getRequest();
		lists = DaoFactory.createFeedBackDao().getAll();
		request.getSession().setAttribute("fbs", lists);
		
		return "getAll";
	}
	
	public String getAllByPage(){
		request = ServletActionContext.getRequest();
		System.out.println("page in");
		List<FeedBack> fbs = null;
		int pageNow = Integer.parseInt(request.getParameter("num"));
		System.out.println("pageNow"+pageNow);
		fbs = DaoFactory.createFeedBackDao().getAll(5,pageNow);
		System.out.println(fbs.size());
		int count = (int) DaoFactory.createFeedBackDao().getCount();
		System.out.println(count);
		if(count%5==0){
			request.getSession().setAttribute("pageCount", count/5);
		}else{
			request.getSession().setAttribute("pageCount", (count/5)+1);
		}
		request.getSession().setAttribute("fbs", fbs);
		request.getSession().setAttribute("pageNow", pageNow);
	
		return "getAll";
	}
	
	public String search(){
		request = ServletActionContext.getRequest();
		HttpSession session = request.getSession(); 
		String method = request.getParameter("method");
		if(method.equals("week")){
			String weektime = request.getParameter("keyword");
			List<FeedBack> fbs = DaoFactory.createFeedBackDao().getByWeek(weektime);
			session.setAttribute("fbs", fbs);
		}
		if(method.equals("teacherName")){
			String teachername = request.getParameter("keyword");
			List<FeedBack> fbs = DaoFactory.createFeedBackDao().getByName(teachername);
			session.setAttribute("fbs", fbs);
		}
		if(method.equals("lesson")){
			String lessonname = request.getParameter("keyword");
			List<FeedBack> fbs =DaoFactory.createFeedBackDao().getByLesson(lessonname);
			session.setAttribute("fbs", fbs);
		}
		return "search";
	}
	
}
